<template>
 <van-icon
 :color="value? '#3296f': '#777'"
 :name="value? 'star':'star-o'"
 @click="onCollect(value)"
 />
</template>

<script>
import { addCollected, removeCollected } from '@/api/article'
import { Toast } from 'vant'
export default {
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return {

    }
  },

  created () {

  },

  methods: {
    async onCollect () {
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '加载中...'
      })
      try {
        if (this.value) {
          await addCollected(this.artId + '')
        } else {
          await removeCollected(this.artId + '')
        }
        this.$emit('input', !this.value)
        Toast.success('收藏成功')
      } catch (err) {
        // console.dir(err)
        Toast.fail('取消收藏')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
